package com.pe1n.yangtaopai.page.type.view.activity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.MyTypeAdapter;
import com.pe1n.yangtaopai.adapter.MyTypeGoodsAdapter;
import com.pe1n.yangtaopai.bean.InnerTypeBean;
import com.pe1n.yangtaopai.page.type.presenter.PresenterImpl;
import com.pe1n.yangtaopai.page.type.view.interfacese.IView;
import com.pe1n.yangtaopai.utils.URLConstant;

import java.util.ArrayList;
import java.util.List;

public class TypeActivity extends AppCompatActivity implements IView {

    private static final String TAG = "TAG";
    private Context mContext = this;
    private PresenterImpl mPresenter;
    private InnerTypeBean bean;
    private List<InnerTypeBean.DataBeanX.DataBean> mList = new ArrayList<>();
    private TextView textView;
    private ImageView imageView;
    private ProgressBar progressBar;
    private XRecyclerView xRecyclerView;
    private MyTypeGoodsAdapter adapter;
    private RelativeLayout titleLayout;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type);

        initData();

        initView();


    }

    private void initView() {

        textView = (TextView) findViewById(R.id.tv_title_type);
        textView.setText(title);
        imageView = (ImageView) findViewById(R.id.iv_bac_type);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        xRecyclerView = (XRecyclerView) findViewById(R.id.xrecview_type);
        xRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 2, LinearLayoutManager.VERTICAL, false));
        adapter = new MyTypeGoodsAdapter(mContext, mList);
        xRecyclerView.setAdapter(adapter);
        xRecyclerView.addHeaderView(LayoutInflater.from(mContext).inflate(R.layout.header_goods_layout, (ViewGroup)findViewById(android.R.id.content),false));
        progressBar = (ProgressBar) findViewById(R.id.progress_bar_type);

        titleLayout = (RelativeLayout) findViewById(R.id.title_layout);
        xRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            boolean isSlidingToBottom = false;
            boolean isSlidingToTop = false;
            boolean isClosed = false;

            //@Override
            //public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            //super.onScrollStateChanged(recyclerView, newState);
            //GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
            // 当不滚动时
            //if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            //获取最后一个完全显示的ItemPosition
            //int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
            //int totalItemCount = manager.getItemCount();

            // 判断是否滚动到底部，并且是向右滚动
            //if (isSlidingToBottom && !isClosed) {
            //    titleLayout.setVisibility(View.GONE);
            //    isClosed = true;
            //    Log.e(TAG,"CLOSE");
            //}

            //if (isSlidingToTop && isClosed) {
            //    titleLayout.setVisibility(View.VISIBLE);
            //    isClosed = false;
            //    Log.e(TAG,"OPEN");
            //}

            //}
            //}

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                int[] location = new int[2];
                titleLayout.getLocationInWindow(location);
                float start = location[1];
                super.onScrolled(recyclerView, dx, dy);

                //dx用来判断横向滑动方向，dy用来判断纵向滑动方向
                if (dy > 0) {
                    //大于0表示正在向下滚动
                    isSlidingToBottom = true;
                    isSlidingToTop = false;

                } else if (dy < -1) {
                    //小于等于0表示停止或向上滚动
                    isSlidingToBottom = false;
                    isSlidingToTop = true;
                }

                if (isSlidingToBottom && !isClosed) {
                    ObjectAnimator.ofFloat(titleLayout, "translationY", titleLayout.getTop(), -titleLayout.getHeight())
                            .setDuration(200)
                            .start();
                    isClosed = true;
                }

                if (isSlidingToTop && isClosed) {
                    ObjectAnimator.ofFloat(titleLayout, "translationY", start - titleLayout.getHeight(), 0)
                            .setDuration(200)
                            .start();
                    isClosed = false;
                }
            }
        });

    }

    private void initData() {

        Intent intent = getIntent();
        int cid = Integer.valueOf(intent.getStringExtra("cid"));
        title = intent.getStringExtra("title");
        String url = String.format(URLConstant.TYPE_URL, cid, 30, 1);
        mPresenter = new PresenterImpl(this);
        mPresenter.loadInnerBeanData(url);

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showLoadFailMsg() {

    }

    @Override
    public void loadInnerTypeBeanData(InnerTypeBean innerTypeBean) {
        bean = innerTypeBean;
        mList.addAll(new ArrayList<>(bean.getData().getData()));
        adapter.notifyDataSetChanged();
    }
}
